<template>
  <el-dialog
    :title="title"
    :visible="dialogVisible"
    width="50%"
    :before-close="handleClose"
  >
    <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="公司名称" prop="name">
        <el-input v-model="formData.name" placeholder="1-50的字符" />
      </el-form-item>
      <el-form-item label="公司编码" prop="code">
        <el-input v-model="formData.code" placeholder="1-50的字符" />
      </el-form-item>
      <el-form-item label="公司负责人" prop="manager">
        <el-select v-model="formData.manager" placeholder="请选择">
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="公司介绍" prop="introduce">
        <el-input
          v-model="formData.introduce"
          type="textarea"
          placeholder="1-300的字符"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getSimple, addDepartrment, getDepartrment, updateDepartrment, getAllDepartrment } from '@/api/departments'

export default {
  props: {
    dialogVisible: {
      type: Boolean,
      required: true
    },
    id: {
      type: String,
      required: true
    }
  },
  data() {
    // 确认code是否重复
    const checkCode = async(rule, value, callback) => {
      const res = await getAllDepartrment()
      const result = res.data.depts.find(item => item.code === value)
      if (result && (this.formData.id !== result.id)) {
        callback('code有重复')
      } else {
        callback()
      }
    }
    return {

      rules: {
        name: [{ required: true, message: '该项不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' }],
        code: [{ required: true, message: '该项不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkCode }
        ],
        manager: [{ required: true, message: '该项不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' }],
        introduce: [{ required: true, message: '该项不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门编码要求1-300个字符', trigger: 'blur' }]
      },
      formData: {
        name: '',
        description: '',
        prople: '',
        details: '',
        id: ''
      },
      title: '新增部门',
      managerList: []
    }
  },
  async created() {
    const { data } = await getSimple()
    this.managerList = data
  },
  methods: {
    // 关闭弹窗
    handleClose() {
      this.reset()
      this.$emit('handleClose')
      this.title = '新增部门'
    },
    // 提交
    async submit() {
      try {
        await this.$refs.form.validate()
        if (this.formData.id) {
          await updateDepartrment(this.formData)
        } else {
          await addDepartrment({ ...this.formData, pid: this.id })
          console.log('新增成功')
        }
        this.$emit('render')
        this.$message.success('操作成功')
        this.handleClose()
      } catch (error) {
        console.log(error)
      }
    },
    // 重置并移除校验
    reset() {
      this.formData = this.$options.data().formData
      this.$refs.form.resetFields()
    },
    // 获取单个组织详情
    async getdeptDetails(id) {
      const res = await getDepartrment(id)
      this.formData = res.data
      this.formData.id = id
      this.title = '修改部门'
    }
  }

}
</script>

<style>
</style>
